//
//  settingsViewController.swift
//  NewLndd
//
//  Created by 邵禹曦 on 2020/1/8.
//  Copyright © 2020 辽宁当代. All rights reserved.
//

import UIKit
// MARK: - 设置
class settingsViewController: ApiViewController {
    
    @IBOutlet weak var xgmm: UIButton!
    @IBOutlet weak var xgzzmm: UIButton!
    @IBOutlet weak var tcdl: UIButton!
    var _comInfo : String?
    override func viewDidLoad() {
        super.viewDidLoad()
        showInfo()

    }
    

}


extension settingsViewController{
    private func showInfo() {
           // GCD
           let group = DispatchGroup()
    
           // 从沙盒中获取参数
            var userPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
            userPath =  (userPath as NSString).appendingPathComponent("data.plist")
            user = NSKeyedUnarchiver.unarchiveObject(withFile: userPath) as? User
Thread.sleep(forTimeInterval: 0.4)

           // 用户验证
           self.u = user?.U!
           self.retcode = user?.RETCODE!
           // 用户验证
           let str = [u,"101",retcode]
            
           //self.isLogin = self._network(str: str as! [String], group: group)
        group.enter()
        NetworkTools.share.check_user(code: str as Array<Any>) { (result, error) in
                      
                          if error != nil {
                              print("no no no")
                              return
                          }
                          

                          if Int(self._retcode(res: result!))! < 0 {
                              self.isLogin = false
                          } else {
                              self.isLogin = true
                          }
                          group.leave()
                      }

           group.notify(queue: DispatchQueue.main) {
               if !self.isLogin {
                   return self.setupVisitorView()
               } else {
                Thread.sleep(forTimeInterval: 0.4)
                   // 2.设置导航栏内容
                   self.setupNavigationBar()
                   //self.showTabBtn()
                    //self.tableShouInfo()
               }
            }
       }
}

extension settingsViewController {
    private func setupNavigationBar() {
        // 监听按钮
        xgmm.addTarget(self, action: #selector(topBtn), for: .touchUpInside)
        xgzzmm.addTarget(self, action: #selector(topBtn1), for: .touchUpInside)
        tcdl.addTarget(self, action: #selector(topBtn2), for: .touchUpInside)
    }
    
    @objc private func topBtn() {
        self.performSegue(withIdentifier: "xgmm", sender: nil)
    }
    
    @objc private func topBtn1() {
        self.performSegue(withIdentifier: "xgzzmm", sender: nil)
    }
    @objc private func topBtn2() {
        loadPsd()
    }
    

    
    // MARK: - 跳转
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // 设置
        if segue.identifier == "xgmm" {
            let secVC = segue.destination as! changePsdViewController
            secVC._comInfo = sender as? String
        }
        
        // 出金
        if segue.identifier == "xgzzmm" {
            let secVC = segue.destination as! changeModifyViewController
            secVC._comInfo = sender as? String
        }
        

    }
}


extension settingsViewController {
    @objc private func  loadPsd() {
       let u = self.u!
       let sid = self.retcode!
       let str = [u, sid]
       NetworkTools.share.user_logoff(code: str as Array<Any>) { (result, error) in
           
           if error != nil {
               print("no no no")
               return
           }
           print(result!)
           if Int(self._retcode(res: result!))! >= 0 {
               if #available(iOS 13.0, *) {
                   self.showMsgbox(_message: "退出登陆成功")
                   self.isLogin = false
                   return self.setupVisitorView()
               }
               
           } else {
               if #available(iOS 13.0, *) {
                   self.showMsgbox(_message: "退出登陆失败")
               }
           }
       }

    }
}
